package com.pat.jrgwt.client;

import com.google.gwt.core.client.GWT;
import com.pat.jrgwt.shared.MyConstants;

/**
 * Class to hold some statistics about Sharpe ratio, both POR1 and POR3
 * 
 * @author Aleksej V.
 *
 */
public class DvRatioStats
{
    public double maxMonthlyReturn = 0;
    public double minMonthlyReturn = 0;

    public double averageReturnOfInvestement = 0;

    public double getAverageMonthlyReturn()
    {
        return totalRecords == 0 ? 0 : totalMonthlyReturn / totalRecords;
    }
    
    public double getAverageLeverageAdaption()
    {
        return totalRecords == 0 ? 0 : totalLeverageAdaption / totalRecords;
    }
    
    public double getAverageRiskPerMonth()
    {
        return totalRecords == 0 ? 0 : totalRiskPerMonth / totalRecords;
    }
    
    public double getAverageAdditionalPerf()
    {
        return totalRecords == 0 ? 0 : totalAdditionalPerf / totalRecords;
    }
    
    public double getAverageDownsideVolatility()
    {
        return totalRecords == 0 ? 0 : totalDownsideVolatility / totalRecords;
    }
    
    public double getAverageVarAdditionalPerfomance()
    {
        return totalRecords == 0 ? 0 : totalVarAdditionalPerfomance / totalRecords;
    }
    
    public double getAverageVarPerfomance()
    {
        return totalRecords == 0 ? 0 : totalVarPerfomance / totalRecords;
    }

    public double drawdown = 0;
    public double performanceYear = 0;
    public double downsideVolatility = 0;
    public double sharpeRatio = 0;
    public double sortinoRatio = 0;

    public double totalMonthlyReturn = 0;
    public double totalLeverageAdaption = 0;
    public double totalRiskPerMonth = 0;
    public double totalAdditionalPerf = 0;
    public double totalDownsideVolatility = 0;
    public double totalVarAdditionalPerfomance = 0;
    public double totalVarPerfomance = 0;
    
    public int totalRecords = 0;

    protected double m_stDevPerformanceYear = Double.NaN;
    public double getStDevPerformanceYear()
    {
        GWT.log("St.Dev. performance year (before) = " + m_stDevPerformanceYear);
        GWT.log("Average VAR performance  = " + getAverageVarPerfomance());
        if( Double.isNaN(m_stDevPerformanceYear) )
        {
            GWT.log("Is NaN!");
            m_stDevPerformanceYear = (getAverageVarPerfomance() < 0) ? 0 : Math.sqrt(getAverageVarPerfomance());
        }
        GWT.log("St.Dev. performance year (after)  = " + m_stDevPerformanceYear);
        return m_stDevPerformanceYear;
    }
    
    protected double m_rootAvgDownsideVolatility = Double.NaN;
    public double getRootAvgDownsideVolatility()
    {
        if( Double.isNaN(m_rootAvgDownsideVolatility) )
        {
            m_rootAvgDownsideVolatility = (getAverageDownsideVolatility() < 0) ? 0 : Math.sqrt(getAverageDownsideVolatility());
        }
        return m_rootAvgDownsideVolatility;
    }

    protected double m_stDevAddPerfomanceYear = Double.NaN;
    public double getStDevAddPerfomanceYear()
    {
        if( Double.isNaN(m_stDevAddPerfomanceYear) )
        {
            m_stDevAddPerfomanceYear = Math.sqrt( getSumAddPerfomanceYear() );
        }
        return m_stDevAddPerfomanceYear;
    }

    public double getSumAddPerfomanceYear()
    {
        return totalVarAdditionalPerfomance / (totalRecords - 1);
    }

    public double getSortinoRatio()
    {
        return (getAverageMonthlyReturn() - getAverageRiskPerMonth()) / getRootAvgDownsideVolatility() * MyConstants.SQRT_12;
    }

}
